package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Sjzdz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午9:54
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class SjzdzDao extends SimpleHibernateTemplate<Sjzdz, Long> {
    public Page<Sjzdz> findSjzdzList(String bm, int pageNo, int pageSize) {
        Page<Sjzdz> page = new Page<Sjzdz>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if (StringUtils.isNotEmpty(bm)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("bm", bm, MatchMode.START)});
        } else
            return this.findAll(page);
    }

    public Sjzdz getSjzdzIdByBz(String bm, String z) {
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("sjzdsy", "sjzdsy")
                .add(Restrictions.eq("sjzdsy.bm", bm))
                .add(Restrictions.eq("z", z)).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
        return (Sjzdz) criteria.uniqueResult();
    }

    public Sjzdz getSjzdzIdByBm(String bm) {
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("sjzdsy", "sjzdsy")
                .add(Restrictions.eq("bm", bm)).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
        return (Sjzdz) criteria.uniqueResult();
    }

    /**
     * 获取所有数据字典值
     *
     * @param id
     * @return
     */
    public List<Sjzdz> findSjzdzById(Long id) {
        return find("from Sjzdz where sjzdsy.id = ?", id);
    }

    public Sjzdz getSjzdzByBz(String z) {
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("sjzdsy", "sjzdsy")
                .add(Restrictions.eq("z", z)).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
        return (Sjzdz) criteria.uniqueResult();
    }
}
